//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.cancel.asyncImage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory.Options;
import android.media.ThumbnailUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CLocalImageStore {
    MemoryCache memoryCache = new MemoryCache();
    FileCache fileCache;
    int imageSize = 100;
    ExecutorService executorService;

    public CLocalImageStore(Context context, String pathName) {
        this.fileCache = new FileCache(context, pathName);
        this.executorService = Executors.newFixedThreadPool(5);
    }

    public void setImageSize(int imageSize) {
        this.imageSize = imageSize;
    }

    public Bitmap getImage(String url) {
        if(url != null && url.length() > 0) {
            Bitmap bitmap = this.memoryCache.get(url);
            if(bitmap != null) {
                return bitmap;
            } else {
                bitmap = this.getBitmap(url);
                this.memoryCache.put(url, bitmap);
                return bitmap;
            }
        } else {
            return null;
        }
    }

    private Bitmap getBitmap(String url) {
        File f = this.fileCache.getFile(url);
        Bitmap b = this.decodeFile(f);
        if(b != null) {
            return b;
        } else {
            try {
                File file = new File(url);
                if(file.exists()) {
                    b = this.decodeFile(file);
                    b = ThumbnailUtils.extractThumbnail(b, this.imageSize, this.imageSize, 2);
                    CompressFormat format = CompressFormat.JPEG;
                    int quality = 100;
                    OutputStream stream = null;
                    stream = new FileOutputStream(f);
                    b.compress(format, quality, stream);
                }

                file = null;
                return b;
            } catch (Exception var8) {
                var8.printStackTrace();
                return null;
            }
        }
    }

    private Bitmap decodeFile(File f) {
        try {
            Options o = new Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), (Rect)null, o);
            int width_tmp = o.outWidth;
            int height_tmp = o.outHeight;

            int scale;
            for(scale = 1; width_tmp / 2 >= this.imageSize && height_tmp / 2 >= this.imageSize; scale *= 2) {
                width_tmp /= 2;
                height_tmp /= 2;
            }

            Options o2 = new Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), (Rect)null, o2);
        } catch (FileNotFoundException var7) {
            return null;
        }
    }

    public void clearCache() {
        this.memoryCache.clear();
        this.fileCache.clear();
    }
}
